<?php
require_once 'common/libs/PHPExcel/Classes/PHPExcel.php';

//新建应用
define("DIR",dirname(__FILE__));
// echo DIR;
define("APPNAME","excel");
ini_set("include_path", ini_get("include_path").PATH_SEPARATOR.DIR."/been");//设置框架所在目录为目录为include_path

include_once "App.class.php";
include_once "Db.php";


$app=App::getInstance();
$app->setConfPath(DIR."/setting");
$db = new Db($app->loadConf("db"));

header("Content-Type: text/html; charset=utf-8");
set_time_limit ( 0 );
ob_end_clean ();
ob_implicit_flush ( 1 );

$path = 'common/upload/excel/';
require_once 'model/trans.php';
$trans = new trans($db);

function addClass($fileName){
	//$fileName = 'class.xls';
	$filePath = $path.$fileName;
	$PHPExcel = new PHPExcel();
	$PHPReader = new PHPExcel_Reader_Excel2007();
	
	//为了可以读取所有版本Excel文件
	if(!$PHPReader->canRead($filePath))
	{
		$PHPReader = new PHPExcel_Reader_Excel5();
		if(!$PHPReader->canRead($filePath))
		{
			echo '未发现Excel文件！';
			return;
		}
	}
	
	//读取Excel文件
	$PHPExcel = $PHPReader->load($filePath);
	
	//选择第一个工作表
	$currentSheet = $PHPExcel->getSheet(0);
	
	//取得一共有多少行
	$allRow = $currentSheet->getHighestRow();
	
	echo '共有'.$allRow.'行<br/>';
	
	//循环读取数据，默认编码是utf8，
	for($currentRow = 2;$currentRow<$allRow;$currentRow++)
	{
	
		$code = $currentSheet->getCell('A'.$currentRow)->getValue();
		$name = $currentSheet->getCell('D'.$currentRow)->getValue();
		$start = $currentSheet->getCell('U'.$currentRow)->getValue();
		$end = $currentSheet->getCell('V'.$currentRow)->getValue();
		$result = $trans->addClass($code, $name, $start, $end, 1);
	
		echo $result;
	}
}

function addTeacher($fileName){

}

function addClassStudent($fileName){
	$fileName = 'class_student.xls';
	$filePath = $path.$fileName;
	$PHPExcel = new PHPExcel();
	$PHPReader = new PHPExcel_Reader_Excel2007();
	
	//为了可以读取所有版本Excel文件
	if(!$PHPReader->canRead($filePath))
	{
		$PHPReader = new PHPExcel_Reader_Excel5();
		if(!$PHPReader->canRead($filePath))
		{
			echo '未发现Excel文件！';
			return;
		}
	}
	
	//读取Excel文件
	$PHPExcel = $PHPReader->load($filePath);
	
	//选择第一个工作表
	$currentSheet = $PHPExcel->getSheet(0);
	
	//取得一共有多少行
	$allRow = $currentSheet->getHighestRow();
	
	echo '共有'.$allRow.'行<br/>';
	
	//循环读取数据，默认编码是utf8，
	for($currentRow = 2;$currentRow<$allRow;$currentRow++)
	{
	
		$class_code = $currentSheet->getCell('A'.$currentRow)->getValue();
		$stu_code = $currentSheet->getCell('D'.$currentRow)->getValue();
		$name = $currentSheet->getCell('E'.$currentRow)->getValue();
		echo 'trans->'.$class_code.'->'.$stu_code.'->'.$name;
		$result1 = $trans->addStudent($stu_code, $name);
		echo '->addStu:'.$result1;
		$result2 = $trans->bindClassStudent($stu_code, $class_code);
	
		echo '->bindClassStu:'.$result2.'<br/>';
	}
}

function addPeriod($fileName){
	$begin = microtime(TRUE);
	$fileName = 'period.xls';
	$filePath = $path.$fileName;
	
	$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_discISAM;
	PHPExcel_Settings::setCacheStorageMethod($cacheMethod);
	
	$PHPExcel = new PHPExcel();
	$PHPReader = new PHPExcel_Reader_Excel2007();
	
	//为了可以读取所有版本Excel文件
	if(!$PHPReader->canRead($filePath))
	{
		$PHPReader = new PHPExcel_Reader_Excel5();
		if(!$PHPReader->canRead($filePath))
		{
			echo '未发现Excel文件！';
			return;
		}
	}
	
	//读取Excel文件
	$PHPExcel = $PHPReader->load($filePath);
	
	//选择第一个工作表
	$currentSheet = $PHPExcel->getSheet(0);
	
	//取得一共有多少行
	$allRow = $currentSheet->getHighestRow();
	
	echo '共有'.$allRow.'行<br/>';
	$end = microtime(TRUE);
	echo '执行了'.($end-$begin).'秒<br/>';
	//循环读取数据，默认编码是utf8，
	for($currentRow = 2;$currentRow<$allRow;$currentRow++)
	{
	
		$section = $currentSheet->getCell('A'.$currentRow)->getValue();
		$course = $currentSheet->getCell('I'.$currentRow)->getValue();
		$class_code = $currentSheet->getCell('C'.$currentRow)->getValue();
		$tea_code = $currentSheet->getCell('K'.$currentRow)->getValue();
		$date = $currentSheet->getCell('E'.$currentRow)->getValue();
		$start_time = $currentSheet->getCell('F'.$currentRow)->getValue();
		$end_time = $currentSheet->getCell('G'.$currentRow)->getValue();
	
		$area = $currentSheet->getCell('AM'.$currentRow)->getValue();
		$classroom = $currentSheet->getCell('J'.$currentRow)->getValue();
		$place = $area.$classroom;
	
		echo 'tran->'.$section.$class_code.$tea_code.$date.$start_time.$end_time.$place;
		$result = $trans->addPeriod($course, $place, $section, $date, $start_time, $end_time, $class_code, $tea_code);
		echo $result.'<br/>';
	}
	$end = microtime(TRUE);
	echo '执行了'.($end-$begin).'秒<br/>';
}




?>